<?php
namespace Common\Model;

/**
 * NginxModel
 * Nginx模型
 *
 * @version $Id$
 * @copyright 2007 - 2013 tcisv.com Ltd.
 * @author cfstory <cfstory@163.com>
 */
class NginxModel extends Model {
    protected $tableName = 'nginx';
    protected $pk        = 'id';

    /**
     * getHourStatsByServerId
     * 根据服务器编号获取当前小时内每分钟的系统负载
     *
     * @param mixed $serverId
     * @return void
     */
    public function getHourStatsByServerId($serverId) {
        $serverId = (int)$serverId;
        $start    = date('Y-m-d H:00:00');
        $end      = date('Y-m-d H:59:59');
        $expr     = array(
            'time2'     => array('between', array($start, $end)),
            'server_id' => $serverId,
        );
        $order    = 'time2 ASC';

        if(!$tmp = $this->where($expr)->order($order)->select()) {
            return false;
        }

        $r = array_fill(0, 60, 0);

        foreach($tmp as $k => $v) {
            $minute = (int)date('i', strtotime($v['time2']));

            $r[$minute]  = (float)$v['connections'];
        }

        return $r;
    }

    /**
     * getDayStatsByServerId
     * 根据服务器编号获取当天内每小时的负载
     *
     * @param mixed $serverId
     * @return void
     */
    public function getDayStatsByServerId($serverId) {
        $serverId   = (int)$serverId;
        $start      = date('Y-m-d 0:00:00');
        $end        = date('Y-m-d 23:59:59');
        $defaultVal = array_fill(0, 24, 0);
        $expr       = array(
            'time2'     => array('between', array($start, $end)),
            'server_id' => $serverId,
        );
        $order = "date_part('hour', time2) ASC";
        $field = "date_part('hour', time2) AS hour, ROUND(AVG(connections)::numeric, 2) AS connections";
        $group = "date_part('hour', time2)";

        if(!$tmp = $this->field($field)->where($expr)->group($group)->order($order)->select()) {
            return false;
        }

        $r = $defaultVal;

        foreach($tmp as $k => $v) {
            $hour     = $v['hour'];
            $r[$hour] = (float)$v['connections'];
        }

        return $r;
    }

    /**
     * getMonthStatsByServerId
     * 根据服务器编号获取当月内每天的负载
     *
     * @param mixed $serverId
     * @return void
     */
    public function getMonthStatsByServerId($serverId) {
        $serverId   = (int)$serverId;
        $start      = date('Y-m-1 0:00:00');
        $end        = date('Y-m-t 23:59:59');
        $defaultVal = array_fill(0, date('t'), 0);
        $expr       = array(
            'time2'     => array('between', array($start, $end)),
            'server_id' => $serverId,
        );
        $order = "date_part('day', time2) ASC";
        $field = "date_part('day', time2) AS day, ROUND(AVG(connections)::numeric, 2) AS connections";
        $group = "date_part('day', time2)";

        if(!$tmp = $this->field($field)->where($expr)->group($group)->order($order)->select()) {
            return false;
        }

        $r = $defaultVal;

        foreach($tmp as $k => $v) {
            $day     = $v['day'] - 1;
            $r[$day] = (float)$v['connections'];
        }

        return $r;
    }

    /**
     * getYearStatsByServerId
     * 根据服务器编号获取当年内每月的负载
     *
     * @param mixed $serverId
     * @return void
     */
    public function getYearStatsByServerId($serverId) {
        $serverId   = (int)$serverId;
        $start      = date('Y-1-1 0:00:00');
        $end        = date('Y-12-31 23:59:59');
        $defaultVal = array_fill(0, 12, 0);
        $expr       = array(
            'time2'     => array('between', array($start, $end)),
            'server_id' => $serverId,
        );
        $order = "date_part('month', time2) ASC";
        $field = "date_part('month', time2) AS month, ROUND(AVG(connections)::numeric, 2) AS connections";
        $group = "date_part('month', time2)";

        if(!$tmp = $this->field($field)->where($expr)->group($group)->order($order)->select()) {
            return false;
        }

        $r = $defaultVal;

        foreach($tmp as $k => $v) {
            $key     = $v['month'] - 1;
            $r[$key] = (float)$v['connections'];
        }

        return $r;
    }

}
